package com.cloudweb.oa.controller.mobile;

import cn.js.fan.util.ParamUtil;
import cn.js.fan.util.StrUtil;
import cn.js.fan.web.SkinUtil;
import com.cloudweb.oa.cache.DepartmentCache;
import com.cloudweb.oa.cache.DeptUserCache;
import com.cloudweb.oa.cache.UserCache;
import com.cloudweb.oa.entity.DeptUser;
import com.cloudweb.oa.entity.User;
import com.cloudweb.oa.security.AuthUtil;
import com.cloudweb.oa.service.HomeService;
import com.cloudweb.oa.service.IUserService;
import com.redmoon.oa.address.AddressDb;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "通讯录（已弃用）")
@Slf4j
@Controller
public class MobileAddressController {

    @Autowired
    private HomeService homeService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    AuthUtil authUtil;

    @Autowired
    IUserService userService;

    @Autowired
    UserCache userCache;

    @Autowired
    DeptUserCache deptUserCache;

    @Autowired
    DepartmentCache departmentCache;

    @RequestMapping(value = "/m/address/listPage")
    public String listPage(Model model) {
        model.addAttribute("nav", homeService.getNav());
        model.addAttribute("realName", userCache.getUser(authUtil.getUserName()).getRealName());
        return "th/m/address/address_list";
    }

    @RequestMapping(value = "/m/address/showPage")
    public String showPage(Model model) {
        model.addAttribute("nav", homeService.getNav());

        int id = ParamUtil.getInt(request, "id", -1);
        if (id==-1) {
            model.addAttribute("msg", SkinUtil.LoadString(request, "err_id"));
            return "th/error/error";
        }

        StringBuilder deptName = new StringBuilder();
        User user = userService.getUserById(id);
        List<DeptUser> list = deptUserCache.listByUserName(user.getName());
        if (list.size() > 0) {
            for (DeptUser deptUser : list) {
                StrUtil.concat(deptName, ",", departmentCache.getDepartment(deptUser.getDeptCode()).getName());
            }
        }
        model.addAttribute("id", id);
        model.addAttribute("user", user);
        model.addAttribute("deptName", deptName);
        return "th/m/address/address_show";
    }
}
